package com.example.controller;


import com.example.common.result.Result;
import com.example.entity.mes.dto.WorkstationAddDto;
import com.example.entity.mes.dto.WorkstationDelDto;
import com.example.entity.mes.dto.WorkstationListDto;
import com.example.entity.mysql.Workstation;
import com.example.mapper.WorkstationMapper;
import com.example.untils.BeanUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 工作站表 前端控制器
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-04-16
 */
@RestController
@RequestMapping("/api/workstation")
@Api(tags = "工作站表")
public class WorkstationController {

    @Autowired
    private WorkstationMapper mapper;

    @ApiOperation("工作站添加")
    @PostMapping("workstationAdd")
    public Result workstationAdd(@RequestBody WorkstationAddDto dto) {
        Workstation workstation = new Workstation();
        BeanUtils.copyPropertiesIgnoreCase(dto, workstation);
        mapper.insert(workstation);
        return Result.success("工作站添加成功");
    }

    @ApiOperation("工作站删除")
    @PostMapping("workstationDel")
    public Result workstationAdd(@RequestBody WorkstationDelDto dto) {
        mapper.deleteById(dto.getWorkstationId());
        return Result.success("工作站添加成功");
    }

    @ApiOperation("工作站列表")
    @PostMapping("workstationList")
    public Result<PageInfo<List<Workstation>>> workstationList(@RequestParam(defaultValue = "0") Integer current,
                                  @RequestParam(defaultValue = "10")  Integer size,
                                  @RequestBody WorkstationListDto dto) {
        PageHelper.startPage(current, size);
        List<Workstation> list = mapper.getWorkstationList(dto);
        PageInfo<List<Workstation>> pageInfo = new PageInfo(list);
        return Result.success(pageInfo);
    }

    @ApiOperation("工作站列表修改")
    @PostMapping("workstationModify")
    public Result workstationModify(@RequestBody Workstation workstation) {
        if(workstation.getWorkstationId() == null) {
            return Result.error("工作站ID为空");
        }
        int b = mapper.updateById(workstation);
        if(b > 0) return Result.success("更新成功");
        return Result.error("更新失败");
    }

}
